var InvoiceOrder = function () {
  var template = /* html */ `
    <div class="invoice-order" v-loading.fullscreen.lock="loading">
      <el-form label-width="auto" size="medium">
        <el-form-item required label="开票金额">
          <el-input-number is-decimal-zero v-model="invoicePrice" class="invoice-input" :min="0" :precision="4" :controls="false"
            placeholder="请输入开票金额"></el-input-number>
        </el-form-item>
        <el-form-item required label="开票日期">
          <el-date-picker v-model="invoiceDate" class="invoice-input" value-format="timestamp" :editable="false" :picker-options="dateOption"
            placeholder="请选择开票日期"></el-date-picker>
        </el-form-item>
      </el-form>
      <div style="text-align:center">
        <el-button size="medium" type="primary" @click="ensure">开票</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      invoice: {
        type: Object,
        required: true
      }
    },

    data: function () {
      var self = this;
      return {
        loading: false,
        invoicePrice: undefined,
        invoiceDate: null,
        dateOption: {
          disabledDate: function (time) {
            return !self.dateLimit(time);
          }
        }
      };
    },

    methods: {
      dateLimit: function (time) {
        return moment(time).isSameOrBefore(moment(), 'day');
      },

      ensure: function () {
        var code = this.checkValid();
        if (code !== '') {
          var msg = i18n.getMsg(`outBalance.error.${code}`);
          this.$alert(msg, '信息');
          return;
        }

        var invoicePrice = this.invoicePrice;
        var invoiceDate = this.invoiceDate;
        var invoice = this.invoice;
        this.loading = true;
        var self = this;
        ajaxRequest('finance/supplementInvoicData', {
          receivePaymentId: invoice.receivePaymentId,
          invoicePrice: invoicePrice,
          invoiceDate: moment(invoiceDate).format('YYYY-MM-DD')
        }).then(function () {
          self.loading = false;
          self.$alert('该收付款单开票成功', '信息', {
            callback: function () {
              self.$emit('invoice-success');
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },

      checkValid: function () {
        if (this.invoicePrice === undefined) {
          return 'invoicePrice';
        }
        if (this.invoiceDate === null) {
          return 'invoiceDate';
        }
        return '';
      }
    },

    template: template
  };
}();
